<?php

namespace app\admin\controller\mall;

use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\model\MallUser;
use think\App;

class User extends AdminController
{
    use Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model=(new MallUser());
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach ($list as $k=>$v){
                $rzinfo =(new MallUserCertification())->where("user_id",'=',$v['id'])->find();
                if($rzinfo){
                    if($rzinfo['status'] == 0){
                        $list[$k]['renzhengstatus'] ='认证中';
                    }elseif($rzinfo['status'] ==1){
                        $list[$k]['renzhengstatus'] ='认证成功';
                    }else{
                        $list[$k]['renzhengstatus'] ='认证失败';
                    }
                }else{
                    $list[$k]['renzhengstatus'] ='未认证';
                }
                if($v['channel_time']>0){
                    $list[$k]['channel_time'] =date('Y-m-d H:i:s',$v['channel_time']);
                }else{
                    $list[$k]['channel_time']='--';
                }
                $uinfo =$this->model->field('id,nickName')->where("invite_code",'=',$v['parent_code'])->find();
                if($uinfo){
                    $list[$k]['parent_name']=$uinfo['nickName'];
                }else{
                    $list[$k]['parent_name']='--';
                }
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    public function getparentcode(){

       $id =$this->request->param('id','0','intval');
       $list = $this->model->field('id,nickName,invite_code')->where("id != '$id'")->select()->toArray();
       $users=[];
       foreach ($list as $k=>$v){
           $users[$k]['id'] =$v['invite_code'];
           $users[$k]['title'] =$v['nickName'];
       }
       $this->success('ok',$users);
    }

}